package com.platform.controller;

import com.platform.entity.SysUserEntity;
import com.platform.service.*;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员、主播Controller
 *
 * @author zhurusen
 * @email 524227263@qq.com
 * @date 2018-06-03 11:20:12
 */
@RestController
@RequestMapping("extend")
public class ExtendController extends AbstractController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private AmountBillService amountBillService;
    @Autowired
    private WithdrawBillService withdrawBillService;

    @RequestMapping("/index")
    public R index() {

        Map<String, Object> params = new HashMap<>();
        params.put("type", 2);
        BigDecimal czAmount = amountBillService.selectByUserIdAndType(params);

        params = new HashMap<>();
        params.put("type", 1);
        BigDecimal xfAmount = amountBillService.selectByUserIdAndType(params);

        params = new HashMap<>();
        params.put("today", true);
        Integer countorder = orderService.queryTotal(params);

        params = new HashMap<>();
        params.put("today", true);
        Integer countuser = userService.queryTotal(params);


        Map<String, Object> re_map = new HashMap<>();
        re_map.put("czAmount", czAmount);
        re_map.put("xfAmount", xfAmount);
        re_map.put("countorder", countorder);
        re_map.put("countuser", countuser);

        return R.ok().put("re_map", re_map);
    }

    @RequestMapping("/highchart")
    @RequiresPermissions("extend:highchart")
    public R highchart() {
        Map<String, Object> re_map = new HashMap<>();

        Map<String, Object> params = new HashMap<>();
        params.put("recharge", true);
        List<Map<String, Object>> list = amountBillService.selectHigh(params);
        params = new HashMap<>();
        params.put("recharge", false);
        List<Map<String, Object>> list2 = amountBillService.selectHigh(params);
        ArrayList<Object> categories = new ArrayList<Object>();
        ArrayList<Object> series = new ArrayList<Object>();
        ArrayList<Object> series2 = new ArrayList<Object>();

        for (Map<String, Object> map : list) {
            categories.add(map.get("click_date"));
            series.add(map.get("count"));
        }

        for (Map<String, Object> map : list2) {
            series2.add(map.get("count"));
        }

        re_map.put("categories", categories);
        re_map.put("series", series);
        re_map.put("series2", series2);
        return R.ok().put("re_map", re_map);
    }


    /**
     * 渠道 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("extend:list")
    public R list(@RequestParam Map<String, Object> params) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-HH-mm");
        params.put("roleId", 2);
        List<SysUserEntity> list = sysUserService.queryList(params);

        List<Map<String, Object>> re_list = new ArrayList<>();
        for (SysUserEntity info : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getUserId());
            map.put("name", info.getUsername());
            map.put("time", dateFormat.format(info.getCreateTime()));
            map.put("downTodayloads", info.getDownloads() != null ? info.getDownloads() : 0);
            map.put("downloads", info.getDownloads() != null ? info.getDownloads() : 0);
            map.put("registerTodayCount", info.getRegisterCount() != null ? info.getRegisterCount() : 0);
            map.put("registerCount", info.getRegisterCount() != null ? info.getRegisterCount() : 0);

            Map<String, Object> param = new HashMap<>();
            param.put("channel", info.getUserId());
            param.put("type", 2);
            map.put("amountToday", amountBillService.selectByChannelToday(param));
            map.put("amount", amountBillService.selectByChannel(param));

            param.put("mobileType", "android");
            map.put("androidCount", userService.queryTotal(param));

            param.put("mobileType", "ios");
            map.put("iosCount", userService.queryTotal(param));
            re_list.add(map);
        }

        return R.ok().put("list", re_list);
    }


    /**
     * 经纪人查看列表
     */
    @RequestMapping("/listAgent")
    public R listAgent(@RequestParam Map<String, Object> params) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-HH-mm");
        params.put("roleId", 3);
        List<SysUserEntity> list = sysUserService.queryList(params);

        List<Map<String, Object>> re_list = new ArrayList<>();
        for (SysUserEntity info : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", info.getUserId());
            map.put("name", info.getUsername());
            map.put("time", dateFormat.format(info.getCreateTime()));


            params = new HashMap<>();
            params.put("agent", info.getUserId());
            params.put("onlineStatus", 1);
            params.put("applicationStatus", 3);
            map.put("online01", userService.queryTotal(params));

            params.put("onlineStatus", 2);
            map.put("online02", userService.queryTotal(params));

            params.put("onlineStatus", 3);
            map.put("online03", userService.queryTotal(params));

            params.put("onlineStatus", 4);
            map.put("online04", userService.queryTotal(params));

            params = new HashMap<>();
            params.put("agent", info.getUserId());
            params.put("applicationStatus", 2);
            map.put("application02", userService.queryTotal(params));

            map.put("withAmountToday", withdrawBillService.selectByAgentId(params));
            map.put("withAmount", withdrawBillService.selectAllByAgentId(params));

            Map<String, Object> param = new HashMap<>();
            param.put("agent", info.getUserId());
            param.put("type", 1);
            map.put("amountToday", amountBillService.selectByChannelToday(param));
            map.put("amount", amountBillService.selectByChannel(param));

            re_list.add(map);
        }

        return R.ok().put("list", re_list);
    }


}
